package com.violet.lease.admin.convert;

import com.violet.lease.model.enums.ItemType;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 *`code`属性到枚举对象
 */
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String code) {
        //ItemType.values() 获取ItemType这个枚举类型中的所有选项值
        return Arrays.stream(ItemType.values())
                .filter(itemType -> code.equals(""+itemType.getCode()))
                .findFirst()
                .orElseThrow(IllegalArgumentException::new) ;
    }
}